package co.fitstart.weapp.api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;

import co.fitstart.entity.credit.Credit;
import co.fitstart.entity.credit.CreditTransaction;
import co.fitstart.entity.user.User;
import co.fitstart.weapp.api.dao.CreditDao;
import co.fitstart.weapp.api.service.CreditService;

/**
 * 
 * @author dan
 *
 */
@Service("creditService")
public class CreditServiceImpl implements CreditService {
    
    @Dao
    private CreditDao creditDao;

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.CreditService#listCreditTransaction(co.fitstart.entity.credit.Credit)
     */
    public List<CreditTransaction> listCreditTransaction(Credit credit) {
        if (credit == null)
            return null;
        return creditDao.listCreditTransaction(credit);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.CreditService#generallyLoad(co.fitstart.entity.user.User)
     */
    public Credit generallyLoad(User user) {
        Credit credit = creditDao.load(user);

        if (credit == null && user.getCredit() > 0) {
            credit = new Credit(user);
            genericDaoSupport.save(credit.increaseCredit(user.getCredit()));
        }

        return credit;
    }
}
